package com.taotao.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.service.ItemService;

import pojo.EasyUIDataGridResult;
import pojo.TaotaoResult;
import utils.IDUtils;
@Service
public class ItemServiceImpl implements ItemService{
	@Autowired
	private TbItemMapper tbItemMapper;
	@Override
	public EasyUIDataGridResult getItemList(int page, int rows) {
		//分页控制
		PageHelper.startPage(page, rows);
		//设置查询条件
		TbItemExample example = new TbItemExample();
		//查询所有(无查询条件)
		List<TbItem> list = tbItemMapper.selectByExample(example);
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setRows(list);
		PageInfo<TbItem> pageInfo =new PageInfo<>(list);
		result.setTotal((int) pageInfo.getTotal());
		return result;
	}
	@Autowired
	private TbItemDescMapper tbItemDescMapper;
	@Override
	public TaotaoResult addItem(TbItem item, String desc) {
		//生产商品id
		long itemId = IDUtils.genItemId();
		//补全TbItem对象的属性
		item.setId(itemId);
		//商品状态，1-正常，2-下架，3-删除
		item.setStatus((byte) 1);
		Date date =new Date();
		item.setCreated(date);
		item.setUpdated(date);
		//// 3、向商品表插入数据
		tbItemMapper.insert(item);
		// 4、创建一个TbItemDesc对象
		TbItemDesc tbItemDesc =new TbItemDesc();
		// 5、补全TbItemDesc的属性
		tbItemDesc.setItemId(itemId);
		tbItemDesc.setItemDesc(desc);
		tbItemDesc.setCreated(date);
		tbItemDesc.setUpdated(date);
		return TaotaoResult.ok();
	}

}
